﻿using SqlSugar;
using System.Reflection;

namespace Zhp.SqlSugar.Trigger.Core
{
    public class TriggerExecute : ITriggerExecute
    {
        private Assembly assembly { get; set; }

        public TriggerExecute(string AssemblyName)
        {
            assembly = Assembly.LoadFrom(Path.Combine(AppContext.BaseDirectory, AssemblyName + ".dll"));
        }
        public void Invoke<TEntity>(ISqlSugarClient db, TriggerEnum option, TEntity insertObj)
        {
            if (assembly == null)
            {
                return;
            }
            var type = assembly.GetTypes().Where(x => x.IsDefined(typeof(TriggerAttribute), true) && x.GetCustomAttribute<TriggerAttribute>().TriggerEntity == typeof(TEntity).Name).FirstOrDefault();
            if (type != null)
            {
                var trigger = Activator.CreateInstance(type);

                var method = type.GetMethod(option.ToString());

                method.Invoke(trigger, new object[] { db, insertObj });
            }
        }

        public void InvokeRange<TEntity>(ISqlSugarClient db, TriggerEnum option, List<TEntity> insertObj)
        {
            if (assembly == null)
            {
                return;
            }
            var type = assembly.GetTypes().Where(x => x.IsDefined(typeof(TriggerAttribute), true) && x.GetCustomAttribute<TriggerAttribute>().TriggerEntity == typeof(TEntity).Name).FirstOrDefault();
            if (type != null)
            {
                var trigger = Activator.CreateInstance(type);

                var method = type.GetMethod(option.ToString());

                method.Invoke(trigger, new object[] { db, insertObj });
            }
        }

        public async Task InvokeAsync<TEntity>(ISqlSugarClient db, TriggerEnum option, TEntity insertObj)
        {
            if (assembly == null)
            {
                return;
            }
            var type = assembly.GetTypes().Where(x => x.IsDefined(typeof(TriggerAttribute), true) && x.GetCustomAttribute<TriggerAttribute>().TriggerEntity == typeof(TEntity).Name).FirstOrDefault();
            if (type != null)
            {
                var trigger = Activator.CreateInstance(type);

                var method = type.GetMethod(option.ToString());

                await (Task)method.Invoke(trigger, new object[] { db, insertObj });
            }
        }

        public async Task InvokeRangeAsync<TEntity>(ISqlSugarClient db, TriggerEnum option, List<TEntity> insertObj)
        {
            if (assembly == null)
            {
                return;
            }
            var type = assembly.GetTypes().Where(x => x.IsDefined(typeof(TriggerAttribute), true) && x.GetCustomAttribute<TriggerAttribute>().TriggerEntity == typeof(TEntity).Name).FirstOrDefault();
            if (type != null)
            {
                var trigger = Activator.CreateInstance(type);

                var method = type.GetMethod(option.ToString());

                await (Task)method.Invoke(trigger, new object[] { db, insertObj });
            }
        }
    }
}
